<?php

// +----------------------------------------------------------------------
// | 记录提交接口
// +----------------------------------------------------------------------
namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use app\common\model\FormLogModel;
use app\common\model\KuaiShouCodeModel;
use app\common\model\KuaiShouConfigModel;
use app\common\model\KuaiShouModel;
use think\facade\Hook;
use think\facade\Validate;
use think\Request;


class FormLog extends Api
{
    /**
     * 获取配置
     * @return void
     */
    public function config()
    {
        $config = [
            'index_title' => config('site.title'),
            'banner' => '//' . $_SERVER['HTTP_HOST'] . config('site.form_banner'),
            'icp' => config('site.web_site_icp'),

        ];
        $this->success('ok', ['config' => $config]);
    }

    /**
     * 提交
     * @param Request $request
     * @return void
     */
    public function submit(Request $request)
    {
        $realname = $request->post('realname/s', '');
        $phone = $request->post('phone/s', '');
        $product = $request->post('product/s', '');

        if (empty($realname)) $this->error('提交失败，姓名不能为空');
        if (empty($product)) $this->error('提交失败，咨询项目不能为空');
        $patternPhone = '/^1[3-9]\d{9}$/';
        if (!preg_match($patternPhone, $phone)) {
            $this->error('提交失败，手机号码格式不正确！');
        }
        $formLogModel = (new FormLogModel());
        $res = $formLogModel->save([
            FormLogModel::PRODUCT => $product,
            FormLogModel::REALNAME => $realname,
            FormLogModel::PHONE => $phone,
            FormLogModel::IP => $request->ip(),
        ]);
        if ($res) {
            $this->success('提交成功！');
        }
        $this->error('提交失败！');
    }


}
